﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SQLIOSimParser
{
    public partial class Settings : Form
    {
        SQLIOSimParserSettings settings;

        public SQLIOSimParserSettings SQLIOSimParserSettings
        {
            get { return settings; }
            set { settings = value; }
        }

        public Settings()
        {
            InitializeComponent();
            LoadSettings();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            btnShowSampleUsage.Text = textBox2.Visible ? "Show sample >>" : "Hide sample <<";
            textBox2.Visible = !textBox2.Visible;
        }

        private void btnSettingsCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOKSettings_Click(object sender, EventArgs e)
        {
            if(SaveSettings())
                this.Close();
        }

        private bool SaveSettings()
        {
            errorProvider1.Clear();

            //settings.PicturePosition = ((BingWorker.PicturePosition)Enum.Parse(typeof(BingWorker.PicturePosition), Orientation));

            settings.ExportDelimiter = txtDelimiter.Text;
            settings.ShareStatistics = chkPrivacy.Checked;

            foreach (Control cont in grpExporType.Controls)
            {
                if (cont is RadioButton && ((RadioButton)cont).Checked)
                    settings.ExportType = ((RadioButton)cont).Tag.ToString();
            }

            Dictionary<string,string> ErrorMessages = settings.ValidateSettings();

            ErrorMessages = settings.ValidateSettings();

            if (ErrorMessages.Count > 0)
            {
                errorProvider1.SetError(btnOKSettings, string.Join("\r\n",
                                                (from MessageText in ErrorMessages
                                                 select MessageText.Key + " " + MessageText.Value).ToArray()));
                return false;
            }
            else
            {
                settings.Save();
                return true;
            }
          }

        private void LoadSettings()
        {
            settings = new SQLIOSimParserSettings();
            chkPrivacy.Checked = settings.ShareStatistics;

            foreach (Control cont in grpExporType.Controls)
            {
                if (cont is RadioButton && ((RadioButton)cont).Tag.ToString() == settings.ExportType)
                    ((RadioButton)cont).Checked = true;
            }

            txtDelimiter.Text = settings.ExportDelimiter;
        }

        private void btnSettingsApply_Click(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (SQLIOSimParserWorker.PostProcessEvent("Feedback", new string[] { txtFeedback.Text }, true))
            {
                lblResult.Text = "Feedback sent successfully.";
                lblResult.ForeColor = Color.Green;
                txtFeedback.Enabled = btnSubmit.Enabled = false;
            }
            else
            {
                lblResult.Text = "Errors during feedback sending.";
                lblResult.ForeColor = Color.Red;
            }

        }


        


    
    
    }
}
